
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsRegisterEntrustEntity;
import com.j2eefast.produce.pms.mapper.PmsRegisterEntrustMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>委托登记</p>
 *
 * @author: summer
 * @date: 2021-05-30 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsRegisterEntrustService extends ServiceImpl<PmsRegisterEntrustMapper, PmsRegisterEntrustEntity> {

	@Resource
	protected PmsRegisterEntrustMapper pmsRegisterEntrustMapper;
	@Resource
	@Lazy
	private SerialService pmsSerialService;

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String inspectionAccount = (String) params.get("inspectionAccount");
		String entrustCode = (String) params.get("entrustCode");
		String projectName = (String) params.get("projectName");
		String projectType = (String) params.get("projectType");
		String quota = (String) params.get("quota");
		String entrustUnit = (String) params.get("entrustUnit");
		String region = (String) params.get("region");
		String registerName = (String) params.get("registerName");
		String taskName = (String) params.get("taskName");
		String beginTime = (String) params.get("beginTime");
		String endTime = (String) params.get("endTime");
		String inspectionAccount2 = (String) params.get("inspectionAccount2");
		String entrustCode2 = (String) params.get("entrustCode2");


		Page<PmsRegisterEntrustEntity> page = pmsRegisterEntrustMapper.findPage(
				new Query<PmsRegisterEntrustEntity>(params).getPage(),
				StrUtil.nullToDefault(inspectionAccount,""),
				StrUtil.nullToDefault(entrustCode,""),
				StrUtil.nullToDefault(projectName,""),
				StrUtil.nullToDefault(projectType,""),
				StrUtil.nullToDefault(quota,""),
				StrUtil.nullToDefault(entrustUnit,""),
				StrUtil.nullToDefault(region,""),
				StrUtil.nullToDefault(registerName,""),
				StrUtil.nullToDefault(taskName,""),
				StrUtil.nullToDefault(beginTime,""),
				StrUtil.nullToDefault(endTime,""),
				StrUtil.nullToDefault(inspectionAccount2,""),
				StrUtil.nullToDefault(entrustCode2,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsRegisterEntrustMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id) {
		Long userId= UserUtils.getUserInfo().getId();

		return pmsRegisterEntrustMapper.updateCheck(id ,userId);

	}
//	public SysUserEntity getSysById(@Param("userId")Long userId){
//		return pmsRegisterEntrustMapper.getSysById(userId);
//	}

	/**
	 * 添加到账认领信息
	 *
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean save(PmsRegisterEntrustEntity register){
//		Long userId= UserUtils.getUserInfo().getId();
		String userName=UserUtils.getUserInfo().getName();
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		register.setCreateTime(date);
		register.setUpdateTime(date);
		register.setCreateBy(userName);
		register.setUpdateBy(userName);
		register.setRegisterName(userName);
		//查询最大编号
		PmsRegisterEntrustEntity pmsRegisterEntrustEntity = pmsRegisterEntrustMapper.selectRegister();
		Map<String ,String> participant=pmsSerialService.formatTitle(pmsRegisterEntrustEntity.getEntrustTotal(),"委托编号序列");//委托编号
		register.setEntrustCode(participant.get("title"));
		register.setEntrustTotal(Long.parseLong(participant.get("total")));

		//查询工程编号
//		PmsRegisterEntrustEntity pmsRegisterEntrustEntity = pmsRegisterEntrustMapper.getRegister();
//		String code=registerCode(pmsRegisterEntrustEntity,1);
//		register.setEntrustCode(code);
//		PmsParticipantEntity pmsParticipantEntity1 =pmsRegisterEntrustMapper.getParticipantCode();
//		String code1=participantCode(pmsParticipantEntity1,2);
	//	taskFrom.setParticipantCode(code1);
		return pmsRegisterEntrustMapper.save(register);

	}

	/**
	 * 生成编号
	 * @param
	 * @return
	 */
//	public String registerCode(PmsRegisterEntrustEntity pmsRegisterEntrustEntity ,int index) {
////		int id = ParamUtils.getInt(s, "-", 0);
//		// 对当前日期进行格式化
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHss");
//		String date = sdf.format(new Date());
//		//DecimalFormat dft = new DecimalFormat(FORMAT_CODE);
//		//StringUtils ParamUtils = new StringUtils();
////		StringBuilder builder = new StringBuilder();
//		System.out.println("111111111111");
//		System.out.println(pmsRegisterEntrustEntity != null);
//		System.out.println("111111111111");
//		if (index == 1) {
//			if ( pmsRegisterEntrustEntity != null) {
//				String[] str = pmsRegisterEntrustEntity.getEntrustCode().split("-");
//				int[] num = new int[str.length];
//				for (int i = 0; i < num.length; i++) {
//					num[i] = Integer.parseInt(str[i]);
//				}
//				int i = num[num.length - 1];
//				StringBuilder builder = new StringBuilder();
//				builder.append("WT-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(i + 1);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			} else {
//				int num = 1;
//				StringBuilder builder = new StringBuilder();
//				builder.append("WT-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(num);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			}
//		}
//		else if (index == 2) {
//			if ( pmsRegisterEntrustEntity != null) {
//				String[] str = pmsRegisterEntrustEntity.getEntrustCode().split("-");
//				int[] num = new int[str.length];
//				for (int i = 0; i < num.length; i++) {
//					num[i] = Integer.parseInt(str[i]);
//				}
//				int i = num[num.length - 1];
//				StringBuilder builder = new StringBuilder();
//				builder.append("CJ-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(i + 1);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			} else {
//				int num = 1;
//				StringBuilder builder = new StringBuilder();
//				builder.append("CJ-");
//				builder.append(date);
//				builder.append("-");
//				builder.append(num);
//				//String code = ParamUtils.getConteactNo(date, i);
//				return builder.toString();
//			}
//
//		}else
//			return "";
//	}

}
